﻿Imports Aricie.DNN.UI.Controls

Public Class PagerTemplate
    Inherits AricieUserControlBase
    Implements ITemplate

    Public Event NextPage As EventHandler
    Public Event PreviousPage As EventHandler
    Public Event ViewPage As EventHandler

    Private _nbPagesMax As Integer = 10
    Private _currentIndex As Integer = 0
    Private _nbTotalPages As Integer = 0
    Private _previousWord As String = ""
    Private _nextWord As String = ""
    Private _pageWord As String = ""
    Private _pagesVisited As List(Of Integer)

    Private _nbsp As String = "&nbsp;&nbsp;"

    Public Sub New(ByVal currentIndex As Integer, ByVal nbTotalPages As Integer, ByVal previousWord As String, ByVal nextWord As String, ByVal pageWord As String, ByVal pagesVisited As List(Of Integer), Optional ByVal nbPagesMax As Integer = 10)
        _nbPagesMax = nbPagesMax
        _currentIndex = currentIndex
        _nextWord = nextWord
        _previousWord = previousWord
        _nbTotalPages = nbTotalPages
        _pageWord = pageWord
        _pagesVisited = pagesVisited
    End Sub

    Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn

        Dim iBegin As Integer = CInt(_currentIndex - (_nbPagesMax / 2)) + 1
        Dim iLast As Integer = CInt(_currentIndex + (_nbPagesMax / 2)) + 1

        container.Controls.Add(New LiteralControl("<div id='pagerDiv' class='pager' align='center'>"))

        'Button Previous
        If Not (_currentIndex = 0) Then

            Dim btnPrevious As New LinkButton
            btnPrevious.ID = "Previous"
            btnPrevious.Text = _previousWord
            btnPrevious.CssClass = "PageNavigation"
            AddHandler btnPrevious.Click, AddressOf _PreviousPage
            container.Controls.Add(btnPrevious)
            container.Controls.Add(New LiteralControl(_nbsp + _nbsp + _nbsp + _nbsp + _nbsp + _nbsp))
        End If


        If iBegin < 1 Then
            iBegin = 1
            iLast = _nbPagesMax
        End If
        If iLast > _nbTotalPages Then
            iLast = _nbTotalPages
            iBegin = _nbTotalPages - _nbPagesMax
        End If


        'Buttons Pages
        For i As Integer = iBegin To iLast
            If i > 0 And i <= _nbTotalPages Then
                Dim btnPage As New LinkButton
                btnPage.ID = i.ToString
                btnPage.Text = _pageWord + " " + i.ToString
                If i = _currentIndex + 1 Then
                    btnPage.CssClass = "PageSelected"
                    btnPage.Enabled = False

                    'VisitedCss est inopérant du fait que le pager est recrée à chaque chargement
                ElseIf _pagesVisited.Contains(i) Then
                    btnPage.CssClass = "PageSelectedVisited"
                End If

                AddHandler btnPage.Click, AddressOf _ViewPage
                container.Controls.Add(btnPage)
                container.Controls.Add(New LiteralControl(_nbsp + _nbsp))
            End If
        Next

        container.Controls.Add(New LiteralControl(_nbsp + _nbsp + _nbsp))

        'Button Next
        If Not (_currentIndex = _nbTotalPages - 1) Then

            Dim btnNext As New LinkButton
            btnNext.ID = "Next"
            btnNext.Text = _nextWord
            btnNext.CssClass = "PageNavigation"
            AddHandler btnNext.Click, AddressOf _NextPage
            container.Controls.Add(btnNext)
        End If

        container.Controls.Add(New LiteralControl("</div>"))

    End Sub

    Private Sub _PreviousPage(ByVal sender As Object, ByVal e As System.EventArgs)
        RaiseEvent PreviousPage(sender, e)
    End Sub

    Private Sub _NextPage(ByVal sender As Object, ByVal e As System.EventArgs)
        RaiseEvent NextPage(sender, e)
    End Sub

    Private Sub _ViewPage(ByVal sender As Object, ByVal e As System.EventArgs)
        RaiseEvent ViewPage(sender, e)
    End Sub

End Class
